#include "e2d/core/render/Utils.h"


bool e2d::Utils::LoadFromModel(e2d::Entity* entity, const fxcc::File& file)
{
	//auto& commonRS = e2d::CommomResources::Inst();

	//if (!commonRS.LoadScene(file.m_RelativePath))
	//{
	//	return false;
	//}
	//auto scene = commonRS.GetScene(file.m_RelativePath);
	//ztclog::info("[entity] try load node scene %s", scene->m_Path.c_str());

	//std::vector<e2d::SkinnedMeshRender*> skinnedMeshRenders;
	//auto flag = LoadFromNode(entity, entity, scene->m_RootNode.get(), scene.get(), skinnedMeshRenders);

	//for (auto it : skinnedMeshRenders)
	//{
	//	if (it)
	//	{
	//		it->OnLoad();
	//	}
	//}
	//// if not this code ,the name will be RootNode
	//entity->m_Name = fxcc::FileUtil::GetNoSuffixName(file.m_FileName);
	//auto& transformNode = entity->Get<e2d::TransformNode>();
	//transformNode.m_Local.m_Rotation = { 0,0,0 };

	//return flag;
	return true;

}


std::shared_ptr<e2d::Entity> e2d::Utils::AddChildFromFile(e2d::Entity* entity, const fxcc::File& file)
{
	auto child = entity->NewChild();

	if (file.IsModel())
	{
		if (LoadFromModel(child.get(), file))
		{
			return child;
		}
	}

	return std::shared_ptr<e2d::Entity>();
}

bool e2d::Utils::LoadFromNode(e2d::Entity* entity, e2d::Entity* rootEntity, const e2d::Node* node, const e2d::ModelScene* scene, std::vector<e2d::SkinnedMeshRender*>& skinnedMeshRenders)

{
	//ztclog::info("load node %s", node->m_Name.c_str());

	//auto& commonRS = e2d::CommomResources::Inst();

	//entity->m_Name = node->m_Name;

	//auto& transformNode = entity->GetOrEmplace<e2d::TransformNode>();
	//transformNode.m_Local = node->m_Offset;

	//if (node->m_Meshes.size())
	//{
	//	auto meshIndex = node->m_Meshes[0];

	//	e2d::DataIndex dataIndex;
	//	dataIndex.m_Path = scene->m_Path;
	//	dataIndex.m_Index = meshIndex;

	//	auto it1 = commonRS.m_MeshDatas.find(dataIndex);
	//	bool skinnedMeshFlag = (it1 != commonRS.m_MeshDatas.end());

	//	if (skinnedMeshFlag)
	//	{
	//		auto& skinnedMeshRender = entity->GetOrEmplace<e2d::SkinnedMeshRender>();
	//		//skinnedMeshRender.m_MeshIndex = it1->first;
	//		//skinnedMeshRender.m_BoneIndex = it1->first;

	//		e2d::DataIndex matDataIndex;
	//		matDataIndex.m_Path = dataIndex.m_Path;
	//		matDataIndex.m_Index = scene->m_MeshDatas[meshIndex]->m_MaterialIndex;
	//		//skinnedMeshRender.m_MaterialView.m_Index.SetMaterial(,) = matDataIndex;

	//		if (rootEntity)
	//		{
	//			skinnedMeshRender.m_RootNode = rootEntity;
	//		}
	//		skinnedMeshRenders.push_back(&skinnedMeshRender);
	//	}
	//	if (!skinnedMeshFlag)
	//	{

	//		auto it2 = commonRS.m_MeshDatas.find(dataIndex);
	//		if (it2 != commonRS.m_MeshDatas.end())
	//		{
	//			auto& fixedRender = entity->GetOrEmplace<e2d::FixedMeshRender>();
	//			fixedRender.OnLoad();


	//			//e2d::DataIndex matDataIndex;
	//			//matDataIndex.m_Path = dataIndex.m_Path;
	//			//matDataIndex.m_Index = scene->m_MeshDatas[meshIndex]->m_MaterialIndex;
	//			//fixedRender.m_MaterialView.m_MaterialMap.m_DataIndex = matDataIndex;

	//		}

	//	}

	//}
	//if (node->m_Cameras.size())
	//{
	//	auto index = node->m_Cameras[0];
	//	e2d::DataIndex dataIndex;
	//	dataIndex.m_Index = index;
	//	dataIndex.m_Path = scene->m_Path;
	//	auto it = commonRS.m_Cameras.find(dataIndex);
	//	if (it != commonRS.m_Cameras.end())
	//	{
	//		auto& cameraView = entity->GetOrEmplace<e2d::CameraView>();
	//		cameraView.m_ProjData = it->second->m_ProjData;

	//		cameraView.OnLoad();
	//	}
	//}
	//if (node->m_Lights.size())
	//{
	//	auto index = node->m_Lights[0];
	//	e2d::DataIndex dataIndex;
	//	dataIndex.m_Index = index;
	//	dataIndex.m_Path = scene->m_Path;

	//	auto it = commonRS.m_LightDatas.find(dataIndex);

	//	if (it != commonRS.m_LightDatas.end())
	//	{
	//		auto& light = entity->GetOrEmplace<e2d::LightSource>();
	//		light.m_EffectData = it->second->m_EffectData;
	//		light.OnLoad();
	//	}

	//}


	//for (auto childNode : node->m_Childern)
	//{
	//	auto child = entity->NewChild();
	//	bool flag = LoadFromNode(child.get(), rootEntity, childNode.get(), scene, skinnedMeshRenders);

	//	if (!flag)
	//	{
	//		ztclog::info("failed load node %s", childNode->m_Name.c_str());
	//		child->m_Exist = false;
	//	}
	//}


	//return true;
	return 0;

}
